{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Sequential_method_to_build_a_neural_network.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.6"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/PacktPublishing/Modern-Computer-Vision-with-PyTorch/blob/master/Chapter02/Sequential_method_to_build_a_neural_network.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:17.305235Z",
          "start_time": "2020-09-25T19:49:17.302498Z"
        },
        "id": "D5_lUQ_JzxNQ"
      },
      "source": [
        "x = [[1,2],[3,4],[5,6],[7,8]]\n",
        "y = [[3],[7],[11],[15]]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:17.611616Z",
          "start_time": "2020-09-25T19:49:17.306365Z"
        },
        "id": "TG0fNwONz6yn"
      },
      "source": [
        "import torch\n",
        "import torch.nn as nn\n",
        "import numpy as np\n",
        "from torch.utils.data import Dataset, DataLoader\n",
        "device = 'cuda' if torch.cuda.is_available() else 'cpu'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:17.616192Z",
          "start_time": "2020-09-25T19:49:17.613054Z"
        },
        "id": "f4-xTYoCz8U9"
      },
      "source": [
        "class MyDataset(Dataset):\n",
        "    def __init__(self, x, y):\n",
        "        self.x = torch.tensor(x).float().to(device)\n",
        "        self.y = torch.tensor(y).float().to(device)\n",
        "    def __getitem__(self, ix):\n",
        "        return self.x[ix], self.y[ix]\n",
        "    def __len__(self): \n",
        "        return len(self.x)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:19.209881Z",
          "start_time": "2020-09-25T19:49:17.617151Z"
        },
        "id": "WeBe83XQz9we"
      },
      "source": [
        "ds = MyDataset(x, y)\n",
        "dl = DataLoader(ds, batch_size=2, shuffle=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:19.213866Z",
          "start_time": "2020-09-25T19:49:19.210841Z"
        },
        "id": "Vcg57P86z_oF"
      },
      "source": [
        "model = nn.Sequential(\n",
        "    nn.Linear(2, 8),\n",
        "    nn.ReLU(),\n",
        "    nn.Linear(8, 1)\n",
        ").to(device)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:21.005946Z",
          "start_time": "2020-09-25T19:49:19.215403Z"
        },
        "id": "7FGa-UWK0BIX",
        "outputId": "570c4f77-ef48-46c7-85b9-49b41eec4088",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        }
      },
      "source": [
        "!pip install torch_summary\n",
        "from torchsummary import summary"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: torch_summary in /home/yyr/anaconda3/lib/python3.7/site-packages (1.4.1)\n",
            "\u001b[33mWARNING: You are using pip version 20.2.2; however, version 20.2.3 is available.\n",
            "You should consider upgrading via the '/home/yyr/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:21.040105Z",
          "start_time": "2020-09-25T19:49:21.011241Z"
        },
        "id": "UVZlHyXh0Fyd",
        "outputId": "1b7c50ea-f954-4a56-8eb0-8095891c943c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 595
        }
      },
      "source": [
        "summary(model, torch.zeros(1,2));"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "==========================================================================================\n",
            "Layer (type:depth-idx)                   Output Shape              Param #\n",
            "==========================================================================================\n",
            "├─Linear: 1-1                            [-1, 8]                   24\n",
            "├─ReLU: 1-2                              [-1, 8]                   --\n",
            "├─Linear: 1-3                            [-1, 1]                   9\n",
            "==========================================================================================\n",
            "Total params: 33\n",
            "Trainable params: 33\n",
            "Non-trainable params: 0\n",
            "Total mult-adds (M): 0.00\n",
            "==========================================================================================\n",
            "Input size (MB): 0.00\n",
            "Forward/backward pass size (MB): 0.00\n",
            "Params size (MB): 0.00\n",
            "Estimated Total Size (MB): 0.00\n",
            "==========================================================================================\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:21.127594Z",
          "start_time": "2020-09-25T19:49:21.044743Z"
        },
        "id": "NDHfUDbW0Lh_",
        "outputId": "c0f4620b-4479-4ecc-d3e2-77e7d067b5d8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "loss_func = nn.MSELoss()\n",
        "from torch.optim import SGD\n",
        "opt = SGD(model.parameters(), lr = 0.001)\n",
        "import time\n",
        "loss_history = []\n",
        "start = time.time()\n",
        "for _ in range(50):\n",
        "    for ix, iy in dl:\n",
        "        opt.zero_grad()\n",
        "        loss_value = loss_func(model(ix),iy)\n",
        "        loss_value.backward()\n",
        "        opt.step()\n",
        "        loss_history.append(loss_value)\n",
        "end = time.time()\n",
        "print(end - start)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.0754392147064209\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:21.130860Z",
          "start_time": "2020-09-25T19:49:21.128656Z"
        },
        "id": "-Y-j0JeW0WKz"
      },
      "source": [
        "val = [[8,9],[10,11],[1.5,2.5]]\n",
        "val = torch.tensor(val).float()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:21.141201Z",
          "start_time": "2020-09-25T19:49:21.132039Z"
        },
        "id": "KdNMIy4u0Xkt",
        "outputId": "1fc3883d-0692-409d-ecb8-d5dd98583285",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "model(val.to(device))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[16.7953],\n",
              "        [20.6512],\n",
              "        [ 4.2647]], device='cuda:0', grad_fn=<AddmmBackward>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:49:21.145210Z",
          "start_time": "2020-09-25T19:49:21.142226Z"
        },
        "id": "0fgQGXEX0YK_",
        "outputId": "a8c760d3-bae4-4954-89cf-8e87d222fc5b"
      },
      "source": [
        "val.sum(-1)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([17., 21.,  4.])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5HF-DDp50YLB"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}